package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.ly.plus.dto.SpecGroupDTO;
import com.ly.plus.entity.SpecGroup;
import com.ly.plus.entity.SpecParam;
import com.netflix.ribbon.proxy.annotation.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 规格业务层
 */
@Service
@Transactional
public class SpecService {

    @Autowired(required = false)
    private SpecGroupMapper groupMapper;

    @Autowired(required = false)
    private SpecParamMapper paramMapper;

    public List<SpecGroup> findSpecGroupsByCid(Long cid) {
        //条件对象
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        //封装条件
        LambdaQueryWrapper<SpecGroup> warpper = Wrappers.lambdaQuery(specGroup);
        List<SpecGroup> specGroupList = groupMapper.selectList(warpper);
        if (CollectionUtils.isEmpty(specGroupList)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specGroupList;
    }

    public List<SpecParam> findSpecParams(Long gid, Long cid, Boolean searching) {
        //规格组id与分类id都为空那-参数无效
        if (gid==null&&cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam specParam = new SpecParam();
        //封装条件
        specParam.setCid(cid);
        specParam.setGroupId(gid);
        specParam.setSearching(searching);
        LambdaQueryWrapper<SpecParam> wrapper = Wrappers.lambdaQuery(specParam);
        //执行查询
        List<SpecParam> specParamList = paramMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(specParamList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specParamList;
    }

    public List<SpecGroupDTO> findSpecGroupDTOsByCid(Long cid) {
        //根据分类id获取规格组
        List<SpecGroup> groups = findSpecGroupsByCid(cid);
        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(groups, SpecGroupDTO.class);
        //根据分类id获取规格参数
        Map<Long, List<SpecParam>> SpecParams = findSpecParams(null, cid, null).stream()
                .collect(Collectors.groupingBy(SpecParam::getGroupId));
        specGroupDTOS.forEach(specGroupDTO -> {
            SpecParams.forEach((key,value)->{
                if (specGroupDTO.getId().equals(key)){
                    specGroupDTO.setParams(value);
                }
            });
        });
        return specGroupDTOS;
    }
}